package com.managerplat.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

import com.managerplat.model.DishInfo;
import com.managerplat.model.Orders;
import com.managerplat.source.Resources;

public class ConsumerTest {
	
	@Test
	public void topicConsumer() throws JMSException
	{
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Connection conn = factory.createConnection();
		conn.start();
		
		Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		Topic topic = session.createTopic("topic_xx001");
		MessageConsumer consumer = session.createConsumer(topic);
		TextMessage message = (TextMessage)consumer.receive();
		
		message.acknowledge();
		
		System.out.println(message.getText());
		
		
		conn.stop();
		conn.close();
	}
	@Test
	public void asynConsumer() throws JMSException, InterruptedException
	{
//		ConnectionFactory factory = new ActiveMQConnectionFactory(Resources.brokerURL);
//		Connection conn = factory.createConnection();
//		conn.start();
//		
//		Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
//		Queue queue = session.createQueue("user_msg23");
		
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Connection conn = factory.createConnection();
		conn.start();
		Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		Queue queue = session.createQueue("user_msg23");
		
		
		
		
		MessageConsumer consumer = session.createConsumer(queue);
		consumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message message) {
				// TODO Auto-generated method stub
				TextMessage msg = (TextMessage) message;
				try {
					System.out.println(msg.getText());
					
					msg.acknowledge();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				/*if(message instanceof TextMessage){
					TextMessage textMsg = (TextMessage)message;
					System.out.println("textMsg" + textMsg);
				}
				else if(message instanceof ObjectMessage)
				{
					ObjectMessage objMsg = (ObjectMessage)message;
					try {
					Orders order = (Orders) objMsg.getObject();
					System.out.println(order.getList().iterator().next().getUserAddr());
					//giveOrderToSellersMQ(order);
					} catch (JMSException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}*/
			}
		});
		
		
	}
	@Test
	public void synConsumer() throws Exception
	{
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Connection conn = factory.createConnection();
		conn.start();
		
		Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		Queue queue = session.createQueue("1_msg");
		
		MessageConsumer consumer = session.createConsumer(queue);
		ObjectMessage objMsg = (ObjectMessage)consumer.receive();
		
		Orders orders = (Orders)objMsg.getObject();
		System.out.println(orders.getList().toString());
		//System.out.println(dish.getDishId()+" "+dish.getDishName()+" "+dish.getSellerId());
		objMsg.acknowledge();
//		TextMessage msg1 = (TextMessage)consumer.receive();
//		System.out.println(msg1.getText());
//		TextMessage msg2 = (TextMessage)consumer.receive();
//		System.out.println(msg2.getText());
//		msg2.acknowledge();
		
		conn.stop();
		conn.close();
		System.out.println("end.....!");
	}
}
